package cn.coderliu.voteweb.common.resultEntity;

import java.io.Serializable;
import java.util.List;

public class SearchResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private List<T> data;
    private Long total;  //总数
    private Integer pageSize; //一页多少个
    private Integer currentPage; //当钱页
    private Integer totalPages;//总页数
    private Integer statusCode=200;
    private String statusMsg;


    public SearchResult(List<T> data, Long total, Integer pageSize) {
        super();
        this.data = data;
        this.total = total;
        this.pageSize = pageSize;

    }

    public SearchResult(List<T> data, Long total) {
        super();
        this.data = data;
        this.total = total;
        this.pageSize = 15;
    }

    public SearchResult(Integer pageSize) {
        super();
        this.pageSize = pageSize;
    }

    public SearchResult() {
        super();
        this.pageSize = 15;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public Long getTotal() {
        if (total == null)
            total = 0l;
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
        if (pageSize != null && pageSize > 0) {
            totalPages = (int) (total / pageSize);
            if (total % pageSize > 0)
                totalPages += 1;
        }
    }

    public int getFirstResult() {
        if (currentPage < 1)
            currentPage = 1;
        return (currentPage - 1) * pageSize;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        if (currentPage >= totalPages)
            currentPage = totalPages;
        this.currentPage = currentPage;
    }

    public Integer getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public boolean hasData() {
        if (data != null && data.size() > 0)
            return true;
        return false;
    }

    public Integer getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(Integer statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusMsg() {
        return statusMsg;
    }

    public void setStatusMsg(String statusMsg) {
        this.statusMsg = statusMsg;
    }
}
